package com.lh.job.controller;

import com.lh.common.annotation.SysLog;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import com.lh.job.entity.ScheduleJobEntity;
import com.lh.job.service.ScheduleJobService;
import com.lh.job.vo.ScheduleJobVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 定时任务
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/job/schedule")
public class ScheduleJobController {
	@Autowired
	private ScheduleJobService scheduleJobService;

	/**
	 * 定时任务列表
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
//	@RequiresPermissions("sys:schedule:list")
	public Result<PageUtils<ScheduleJobEntity>> list(@RequestParam Map<String, Object> params){
		PageUtils page = scheduleJobService.queryPage(params);

		return Result.ok(page);
	}

	/**
	 * 定时任务信息
	 */
	@RequestMapping(value = "/info/{jobId}",method = RequestMethod.GET)
//	@RequiresPermissions("sys:schedule:info")
	public Result<ScheduleJobEntity> info(@PathVariable("jobId") Long jobId){
		ScheduleJobEntity schedule = scheduleJobService.getById(jobId);

		return Result.ok(schedule);
	}

	/**
	 * 保存定时任务
	 */
	@SysLog("保存定时任务")
	@RequestMapping(value = "/save")
//	@RequiresPermissions("sys:schedule:save")
	public Result save(@RequestBody ScheduleJobEntity scheduleJob){
		scheduleJobService.saveJob(scheduleJob);

		return Result.ok();
	}

	/**
	 * 修改定时任务
	 */
	@SysLog("修改定时任务")
	@RequestMapping(value = "/update")
//	@RequiresPermissions("sys:schedule:update")
	public Result update(@RequestBody ScheduleJobEntity scheduleJob){

		scheduleJobService.update(scheduleJob);

		return Result.ok();
	}

	/**
	 * 删除定时任务
	 */
	@SysLog("删除定时任务")
	@RequestMapping(value = "/delete")
//	@RequiresPermissions("sys:schedule:delete")
	public Result delete(@RequestBody ScheduleJobVo vo){
		scheduleJobService.deleteBatch(vo.getIds());

		return Result.ok();
	}

	/**
	 * 立即执行任务
	 */
	@SysLog("立即执行任务")
	@RequestMapping(value = "/run")
	@RequiresPermissions("sys:schedule:run")
	public Result run(@RequestBody ScheduleJobVo vo){
		scheduleJobService.run(vo.getIds());

		return Result.ok();
	}

	/**
	 * 暂停定时任务
	 */
	@SysLog("暂停定时任务")
	@RequestMapping(value = "/pause")
//	@RequiresPermissions("sys:schedule:pause")
	public Result pause(@RequestBody ScheduleJobVo vo){
		scheduleJobService.pause(vo.getIds());

		return Result.ok();
	}

	/**
	 * 恢复定时任务
	 */
	@SysLog("恢复定时任务")
	@RequestMapping(value = "/resume")
//	@RequiresPermissions("sys:schedule:resume")
	public Result resume(@RequestBody ScheduleJobVo vo){
		scheduleJobService.resume(vo.getIds());

		return Result.ok();
	}

}
